﻿using Model;
using MyWeb.API.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestCoupon.Model;
using WebIOCService.Interface;
using Ymatou.CommonService;
using Ymatou.Coupon.Manage.Contract;
using Ymatou.Coupon.Manage.Contract.Conversion;

namespace WebIOCService.Implement
{
    class CouponService : ICouponService
    {
        /// <summary>
        /// 获取兑换码ID
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public int GetConversionBillByBatchReq(string batchCode)
        {
            try
            {
                GetConversionBillByBatchReq request = new GetConversionBillByBatchReq();
                request.BatchCode = batchCode;

                ApplicationLog.Info(string.Format("CouponService.GetConversionBillByBatchReq 请求正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(request)));
                var response = ConversionClient.GetIdyBatch(request);
                ApplicationLog.Info(string.Format("CouponService.GetConversionBillByBatchReq 返回正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(response)));
                if (response != null && response.Data != null)
                    return response.Data.ConversionBillId;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("CouponService.GetConversionBillByBatchReq 异常：", ex);
            }
            return 0;
        }

        /// <summary>
        /// 获取兑换码生成状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskStatusInfo GetConversionCodeGenerateStatusReq(int conversionBillId)
        {
            try
            {
                GetConversionCodeGenerateStatusReq request = new GetConversionCodeGenerateStatusReq();
                request.ConversionBillId = conversionBillId;

                ApplicationLog.Info(string.Format("CouponService.GetConversionCodeGenerateStatusReq 请求正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(request)));
                var response = ConversionClient.GetGenerateStatus(request);
                ApplicationLog.Info(string.Format("CouponService.GetConversionCodeGenerateStatusReq 返回正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(response)));
                if (response != null && response.Data != null)
                    return response.Data.GenerateStatus;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("CouponService.GetConversionCodeGenerateStatusReq 异常：", ex);
            }
            return 0;
        }

        /// <summary>
        /// 获取兑换码生成数量
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GetConversionBillInfoResp GetConversionBillInfoReq(int conversionBillId)
        {
            try
            {
                GetConversionBillInfoReq request = new GetConversionBillInfoReq();
                request.ConversionBillId = conversionBillId;

                ApplicationLog.Info(string.Format("CouponService.GetConversionBillInfoReq 请求正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(request)));
                var response = ConversionClient.GetById(request);
                ApplicationLog.Info(string.Format("CouponService.GetConversionBillInfoReq 返回正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(response)));
                if (response != null)
                    return response.Data;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("CouponService.GetConversionBillInfoReq 异常：", ex);
            }
            return null;
        }

        /// <summary>
        /// 获取兑换码生成方式(1：自定义)
        /// </summary>
        /// <param name="conversionBillId"></param>
        /// <returns>1：自定义</returns>
        public int GetGenerateMethod(int conversionBillId)
        {
            var data = GetConversionBillInfoReq(conversionBillId);
            if (data != null)
                return data.GenerateMethod.GetHashCode();
            return 0;
        }

        /// <summary>
        /// 获取兑换码生成数量
        /// </summary>
        /// <param name="conversionBillId"></param>
        /// <returns></returns>
        public int GetGenerateQuantity(int conversionBillId)
        {
            var data = GetConversionBillInfoReq(conversionBillId);
            if (data != null)
                return data.GenerateQuantity;
            return 0;
        }

        /// <summary>
        /// 分页获取兑换码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<Coupon> GetConversionCodesByPaging(int conversionBillId, int pageIndex, int pageSize)
        {
            try
            {
                GetConversionCodesByPagingReq request = new GetConversionCodesByPagingReq();
                request.ConversionBillId = conversionBillId;
                request.PageIndex = pageIndex;
                request.PageSize = pageSize;

                ApplicationLog.Info(string.Format("CouponService.GetConversionCodesByPaging 请求正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(request)));
                var response = ConversionClient.getConversionCodesByPaging(request);
                ApplicationLog.Info(string.Format("CouponService.GetConversionCodesByPaging 返回正文：{0}", Newtonsoft.Json.JsonConvert.SerializeObject(response)));
                if (response != null && response.Data != null && response.Data.ConversionCodes != null && response.Data.ConversionCodes.Count > 0)
                {
                    return response.Data.ConversionCodes.Select(x => new Coupon()
                    {
                        CouponCode = x
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("CouponService.GetConversionCodesByPaging 异常：", ex);
            }
            return null;
        }

        public CouponBatch GetCouponBatch(int batchId, out string msg)
        {
            msg = "";
            CouponBatch result = new CouponBatch();
            var servieUrl = ConfigurationManager.AppSettings["CouponBatchServiceUrl"];
            CouponRequest requst = new CouponRequest
            {
                BatchId = batchId
            };
            var response = ApiClient.Execute(requst, servieUrl, 100000);
            if (response != null && response.RetCode == 200)
            {
                result = response.RetData.CouponBatch;
            }
            else
            {
                msg = response.RetMsg;
            }
            return result;
        }

        public List<CouponBatch> GetCouponBatchs(List<int> batchIds, out string msg)
        {
            msg = "";
            List<CouponBatch> result = new List<CouponBatch>();
            var servieUrl = ConfigurationManager.AppSettings["CouponBatchServiceUrl"];
            CouponListRequest requst = new CouponListRequest
            {
                BatchIds = batchIds
            };
            var response = ApiClient.Execute(requst, servieUrl, 100000);
            if (response != null && response.RetCode == 200)
            {
                result = response.RetData.CouponBatchList;
            }
            else
            {
                msg = response.RetMsg;
            }
            return result;
        }
    }
}
